package com.coocaa.ops.admin.v1.policygroup.provider.impl;

import com.coocaa.ops.admin.v1.homepage.biz.impl.HomepageInfoBizImpl;
import com.coocaa.ops.admin.v1.policygroup.biz.impl.PolicyGroupChipModelConfigBizImpl;
import com.coocaa.ops.admin.v1.policygroup.biz.impl.PolicyGroupInfoBizImpl;
import com.coocaa.ops.admin.v1.policygroup.convert.PolicyGroupQueryConverter;
import com.coocaa.ops.admin.v1.policygroup.model.CheckModelChipDataModel;
import com.coocaa.ops.admin.v1.policygroup.model.query.PolicyGroupInfoModel;
import com.coocaa.ops.admin.v1.policygroup.provider.IPolicyGroupInfoQueryProvider;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageInfoEntity;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupChipModelConfigEntity;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupInfoEntity;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author bjh
 * @description 策略组信息原始表-查询provider接口实现
 * @date : 2020-03-12 14:46:04
 */
@Service
public class PolicyGroupInfoQueryProviderImpl implements IPolicyGroupInfoQueryProvider {
    @Autowired
    private PolicyGroupInfoBizImpl thisBizImpl;
    @Autowired
    private PolicyGroupChipModelConfigBizImpl chipModelConfigBiz;
    @Autowired
    private HomepageInfoBizImpl homeBiz;


    /**
     * 获取详情信息
     *
     * @param id
     * @param version
     * @return
     */
    @Override
    public PolicyGroupInfoEntity getDetailInfo(Integer id, Integer version) {
        PolicyGroupInfoEntity entity = thisBizImpl.getEntityByIdAndVersion(id, version);
        HomepageInfoEntity normalHomepage = (HomepageInfoEntity) homeBiz.queryById(entity.getHomepageId());
        entity.setNormalHomepage(normalHomepage);
        List<PolicyGroupChipModelConfigEntity> rlsModelChipList = chipModelConfigBiz.queryChipModelList(id, entity.getCurrentVersion());
        entity.setRlsModelChipList(rlsModelChipList);
        return entity;
    }

    /**
     * 分页数据-菜单自己使用
     *
     * @param model
     * @return
     */
    @Override
    public PageResultModel<PolicyGroupInfoEntity> pageList(PolicyGroupInfoModel model) {
        Map<String, Object> params = PolicyGroupQueryConverter.getQuery(model);
        PageResultModel<PolicyGroupInfoEntity> pageResult = thisBizImpl.getCommonPageList(params, true);
        return pageResult;
    }

    /**
     * 多个使用逗号分隔
     * 15U_5S02
     *
     * @param modelChip
     */
    @Override
    public List<CheckModelChipDataModel> listPolicyGroupByModelChip(String modelChip) {
        String[] modelChipArray = modelChip.split(",");
        List<CheckModelChipDataModel> result = InstanceUtil.newArrayList();
        for (String modelChipStr : modelChipArray) {
            if (StringUtils.isBlank(modelChipStr)) {
                continue;
            }
            String[] modelChipPair = modelChipStr.split("_");
            if (ArrayUtils.isEmpty(modelChipPair)) {
                continue;
            }

            if (modelChipPair.length == 1) {
                CheckModelChipDataModel checkModelChip = new CheckModelChipDataModel(modelChipStr);
                result.add(checkModelChip);
            } else {
                List<PolicyGroupInfoEntity> policyGroupInfoEntityList = thisBizImpl.getMapper().queryListForModelChip(modelChipPair[0], modelChipPair[1]);
                CheckModelChipDataModel checkModelChip = new CheckModelChipDataModel(modelChipStr, policyGroupInfoEntityList);
                result.add(checkModelChip);
            }
        }
        return result;
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/


}
